package com.sun.dream.fish.secret.hash;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Example {
    public static void main(String[] args) {
        String input = "Hello World";

        try {
            String md5Hash = getMD5Hash(input);
            System.out.println("MD5 hash of \"" + input + "\": " + md5Hash);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    public static String getMD5Hash(String input) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] messageDigest = md.digest(input.getBytes());

        BigInteger no = new BigInteger(1, messageDigest);

        StringBuilder hashText = new StringBuilder(no.toString(16));

        while (hashText.length() < 32) {
            hashText.insert(0, "0");
        }

        return hashText.toString();
    }
}